$(document).ready(function(){
	$(".feed-piclist ul a").css("cursor","url(styles/big.cur),auto");
	$(".feed-piclist ul a").fancyzoom();
	
	$(".feed-item").each(function(){
		var feedid = $(this).find(".feed-id").val();
		var html_share = "<ul>";
		html_share += "<li style='display:inline-block;margin:0 5px'><a href='"+"share.php?feedid="+feedid+"&webid=sina'>新浪微博</a></li>";
		html_share += "<li style='display:inline-block;margin:0 5px'><a href='"+"share.php?feedid="+feedid+"&webid=qt'>腾讯微博</a></li>";
		html_share += "<li style='display:inline-block;margin:0 5px'><a href='"+"share.php?feedid="+feedid+"&webid=qz'>QQ空间</a></li>";
		html_share +="</ul>";
		
		$(this).find(".att_share").tooltip({html:html_share});
	});
	
});
function feed_delete(obj){
	var feed = $(obj).parents(".feed-item");
	var feed_id = feed.find("input.feed-id").val();
	if(feed_id!=null && feed_id!=""){
		if(confirm("确定要删除这条分享吗？")){
			$.get("a.php?a=d_f&feed_id="+feed_id,function(data){
				if(data==1){
					feed.remove();
				}else{
					alert("该分享已经被删除或无权删除！");
				}
			});
		}
	}
}
function feed_mouseover(obj_feed){
	$(obj_feed).find(".att_delete").show();
	$(obj_feed).find(".feed-msg-opt").css("visibility","visible");
}
function feed_mouseout(obj_feed){
	$(obj_feed).find(".att_delete").hide();
	$(obj_feed).find(".feed-msg-opt").css("visibility","hidden");
}
/*
$(function(){
	  window.CardHoverTimer=setTimeout(function(){return true;},10);
});
*/
function comment_add(obj){
	var feed = $(obj).parents(".feed-item");
	var feed_id = feed.find(".feed-id").val();
	var textarea = feed.find(".com-my textarea");
	var mycomment = textarea.val();
	mycomment = mycomment.trim();
	if(mycomment.length==0){
		textarea.effect("highlight");
		return;
	}
	
	if(mycomment.length>140){
		alert("评论不得超过140个字");
		textarea.focus();
		return;
	}
	
	mycomment = encodeURIComponent(mycomment);
	$.get("a.php?a=feed_com_add&refer_id="+feed_id+"&com="+mycomment,function(data){
		if(data==1){
			textarea.val("");
			comments_load(obj,1);
		}else{
			alert(data);
		}
	});
}
function comments_load(obj,page){
	var feed = $(obj).parents(".feed-item");
	var feed_id = feed.find("input.feed-id").val();
	$.getJSON("a.php?a=feed_com&feed_id="+feed_id+"&page="+page,function(data){
		if(data.data.length>0){
			feed.find(".com-list").empty();
			//comments
			var comlist = data.data;
			for(var i=0;i<comlist.length;i++){
				var com = comlist[i];
				var item = $("#feeds").find(".com-item-sample").clone(true);
				item.css("display","");
				if(com.avatar_fid_s){
					item.find("img").attr("src","avatar.php?fid="+com.avatar_fid_s+"&s");
				}else{
					item.find("img").attr("src","avatar.php?s");
				}
				item.find(".com-con1").html("<a href='u.php?uid="+com.uid+"'>"+com.uname+"</a>：<span style='display:inline'>"+com.msg+"</span><small>("+com.publish_time+")</small></div>");
				if(com.my){
					item.find(".com-con2").html("<a href='#u' onclick=\"comment_replay(this);\">回复</a><a href='#u' onclick=\"comment_delete(this);\">删除</a>");
				}else{
					item.find(".com-con2").html("<a href='#u' onclick=\"comment_replay(this);\">回复</a>");
				}
				item.removeClass("com-item-sample");
				item.addClass("com-item");
				item.attr("id",com.id);
				item.appendTo(feed.find(".com-list"));
			}
			
			//comments navigation
			feed.find(".com-nav").empty();
			var totalpage = Math.ceil(data.total/data.pagesize);
			if(totalpage>=2){
				var html = "";
				if(totalpage<=7){
					for(var k=1;k<=totalpage;k++){
						if(k==page){
							html+="<a href='#u' onclick='javascript:comments_load(this,"+k+");'><b>"+k+"</b></a>";
						}else{
							html+="<a href='#u' onclick='javascript:comments_load(this,"+k+");'>"+k+"</a>";
						}
					}
				}else{
					if(page-1>3){
						html+="<a href='#u' onclick='javascript:comments_load(this,1);'>1</a><span>...</span>";
					}
					for(var k=page-3;k<=page;k++){
						if(k<1)continue;
						if(k==page){
							html+="<a hre='#u' onclickf='javascript:comments_load(this,"+k+");'><b>"+k+"</b></a>";
						}else{
							html+="<a href='#u' onclick='javascript:comments_load(this,"+k+");'>"+k+"</a>";
						}
					}
					for(var k=page+1;k<=totalpage;k++){
						if(k-page<=3){
							html+="<a href='#u' onclick='javascript:comments_load(this,"+k+");'>"+k+"</a>";
						}
					}
					if(totalpage-page>3){
						html+="<span>...";
					}
				}
				if(page>1){
					html="<a href='#u' onclick='javascript:comments_load(this,"+(page-1)+");'>上一页</a>"+html;
				}
				if(page<totalpage){
					html=html+"<a href='#u' onclick='javascript:comments_load(this,"+(page+1)+");'>下一页</a>";
				}
				feed.find(".com-nav").html(html);
			}
		}
	});
}
function comments_show(obj){
	var feed = $(obj).parents(".feed-item");
	var feed_id = feed.find("input.feed-id").val();
	if(feed.find(".feed-comlist").css("display")!='none'){
		$(obj).removeClass("att_selected");
		feed.find(".feed-comlist").hide();
	}else{
		$(obj).addClass("att_selected");
		feed.find(".feed-comlist").show();
		comments_load(obj,1);
	}
	feed.find(".feed-comlist textarea").focus();
}
function comment_check(obj){
	var length = obj.value.trim().length;
	if(length<=140){
		$(obj).parents(".feed-comlist").find(".com-tip").html((140-length)+"/140");
	}else{
		$(obj).parents(".feed-comlist").find(".com-tip").html("<font color=red>"+(140-length)+"</font>/140");
	}
}
function comment_delete(obj){
	var comment_id = $(obj).parents(".com-item").attr("id");
	$.get("a.php?a=com_del&id="+comment_id,function(data){
		if(data=='1'){
			$(obj).parents(".com-item").remove();
		}else{
			alert(data);
		}
	});
}
function comment_replay(obj){
	var uname = $(obj).parents(".com-con").find(".com-con1 a").text();
	var com = $(obj).parents(".com-con").find(".com-con1 span").text();
	$(obj).parents(".feed-comlist").find("textarea").val("//@"+uname+":"+com);
	comment_check($(obj).parents(".feed-comlist").find("textarea")[0]);
	$(obj).parents(".feed-comlist").find("textarea").focus();
}
function forward_show(obj){
	var feed = $(obj).parents(".feed-item");
	var lx = feed.find("input.feed-lx").val();
	if(lx=='1'){
		var feed_id = feed.find("input.refer-id").val();
	}else if(lx=='0'){
		var feed_id = feed.find("input.feed-id").val();
	}
	if(!feed_id){
		alert("该条分享不存在或已被删除");
		return false;
	}
	var dialog = $("#feeds").find(".forward-sample");
	dialog.find("input").val(feed_id);
	dialog.find("textarea").val("");

	var feed =  $(obj).parents(".feed-item");
	feed.find(".feed-msg-opt").css("visibility","hidden");
	var lx = feed.find(".feed-lx").val();
	var refer = dialog.find(".forward-refer");
	refer.empty();
	
	var msg = null;
	var reslist = null;
	if(lx=="0"){
		var msg = feed.find(".feed-msg").clone(true);
		var reslist = feed.find(".feed-reslist").clone(true);
		var piclist = feed.find(".feed-piclist").clone(true);
	}else if(lx=='1'){
		var msg = feed.find(".feed-refer").find(".feed-msg").clone(true);
		var reslist = feed.find(".feed-refer").find(".feed-reslist").clone(true);
		var content = "//@"+feed.find(".feed-msg-cur a.feed-msg-uname").text()+": "+feed.find(".feed-msg-cur span").text();
		dialog.find("textarea").val(content);
	}
	forward_check(dialog.find("textarea")[0]);
	refer.append(msg);
	refer.append(reslist);
	
	dialog.openDialog({title:"转发"});
	
	dialog.find("textarea").focus();
}
function forward_ok(obj){
	var dialog = $(obj).parents(".forward-sample");
	var msg = dialog.find("textarea").val().trim();
	if(msg.length>140){
		alert("输入不得超过140个字");
		dialog.find("textarea").focus();
		dialog.find("textarea").effect("highlight");
		return false;
	}
	var feed_id = dialog.find("input").val();
	dialog.closeDialog();
	$.getJSON("a.php?a=forward_add&refer_id="+feed_id+"&msg="+encodeURIComponent(msg),function(data){
		if(data.success){
			feed_load(data.id,true);
		}else{
			alert(data.error);
		}
	});
}

function forward_check(obj){
	var length = obj.value.trim().length;
	if(length<=140){
		$(obj).parents(".forward-sample").find(".tip").html((140-length)+"/140");
	}else{
		$(obj).parents(".forward-sample").find(".tip").html("<font color=red>"+(140-length)+"</font>/140");
	}
}
/*
function feeds(url,nofeed_msg){
	window.scrollTo(0,0);
	
	$("#feeds #feeds-tip").css("display","block");
	$("#feeds #feeds-tip").html("数据加载中，请稍候。。。");

	$("#feeds #feeds-container").css("display","none");
	$("#feeds #feed-container").empty();

	$("#feeds .digg").css("display","none");
	$("#feeds .digg").empty();
	
	$.getJSON(url,function(data){
		if(data.success=='1'){
			if(data.feeds.length>0){
				for(var i=0;i<data.feeds.length;i++){
					feed_load(data.feeds[i]);
				}
				$("#feeds #feeds-container").css("display","block");
				$("#feeds .digg").css("display","block");
				$("#feeds .digg").html(pagenav(url,data.page,data.pagesize,data.total));
			}else{
				$("#feeds #feeds-tip").html(nofeed_msg?nofeed_msg:"没有数据");
			}
		}else{
			if($("#feeds #feeds-tip").css("display")=="none"){
				$("#feeds #feeds-tip").css("display","block");
			}
			$("#feeds #feeds-tip").html(data.error);
		}
	});

}
function feed_load(feedid,prepend){
	var container = $("#feed-container");
	var template = $("#feeds .template-feed-item").clone(true);
	template.removeClass("template-feed-item");
	if(prepend){
		template.prependTo(container);
	}else{
		template.appendTo(container);
	}
	if(feedid){
		$.getJSON("feeds.php?nav=feed&feedid="+feedid,function(data){
			if(data.success){
				var feed = data.feed;
				
				template.find("dt img").attr("src","avatar.php?fid="+(feed.avatar_fid_m!=null?feed.avatar_fid_m:null)+"&m");
				template.find("input.feed-lx").val(feed.lx);
				template.find("input.feed-id").val(feed.id);
				template.find(".feed-msg-cur a.feed-msg-uname").attr("href","u.php?uid="+feed.uid);
				template.find(".feed-msg-cur a.feed-msg-uname").text(feed.uname);
				template.find(".feed-msg-cur span").text(feed.msg?feed.msg:"无");
				
				template.find(".att-cur .att_publish_time").attr("href","u.php?feed="+feed.id);
				template.find(".att-cur .att_publish_time").html(feed.publish_time);
				
				if(!data.myuid || data.myuid!=feed.uid){
					template.find(".att-cur .att_delete").remove();
				}
				
				template.find(".att-cur .att_forward").text("转发("+feed.forward+")");
				template.find(".att-cur .att_comment").text("评论("+feed.comment+")");
				if(feed.lx=='1'){//转发
					template.find(".feed-reslist-cur").remove();
					template.find(".tags-cur").remove();
					template.find(".feed-piclist-cur").remove();
					
					if(feed.refer){
						template.find("input.refer-id").val(feed.refer.id);
						
						template.find(".feed-refer .feed-msg a.feed-msg-uname").attr("href","u.php?uid="+feed.refer.uid);
						template.find(".feed-refer .feed-msg a.feed-msg-uname").text(feed.refer.uname);
						template.find(".feed-refer .feed-msg span").text(feed.refer.msg?feed.refer.msg:"无");
						if(feed.refer.reslist){
							for(var i=0;i<feed.refer.reslist.length;i++){
								var li = $("<li></li>");
								var img = $("<img/>");
								var a = $("<a></a>");
								
								if(feed.refer.reslist[i].lx=='torrent'){
									img.attr("src",data.weburl+"/resource/res_torrent_20.png");
								}else if(feed.refer.reslist[i].lx=='ed2k'){
									img.attr("src",data.weburl+"/resource/res_ed2k_20.png");
								}else if(feed.refer.reslist[i].lx=='magnet'){
									img.attr("src",data.weburl+"/resource/res_magnet_20.png");
								}else if(feed.refer.reslist[i].lx=='thunder'){
									img.attr("src",data.weburl+"/resource/res_thunder_20.png");
								}
								a.attr("href","download.php?id="+feed.refer.reslist[i].id+"&feedid="+feed.id);
								a.attr("target","_blank");
								a.text(feed.refer.reslist[i].name);
								
								li.append(img);
								li.append(a);
								if(feed.refer.reslist[i].size){
									li.append("<font color=gray>&nbsp;("+feed.refer.reslist[i].size+"&nbsp;"+feed.refer.reslist[i].download+"次下载)</font>");
								}else{
									li.append("<font color=gray>&nbsp;("+feed.refer.reslist[i].download+"次下载)</font>");
								}
								
								template.find(".feed-refer .feed-reslist").append(li);
							}
						}
						if(feed.refer.tags){
							for(var i=0;i<feed.refer.tags.length;i++){
								template.find(".feed-refer .tags").append("<a href='"+data.weburl+"/search.php?lx=all&sj=all&skey="+feed.refer.tags[i]+"'>"+feed.refer.tags[i]+"</a>");
							}
						}else{
							template.find(".feed-refer .tags").remove();
						}
						if(feed.refer.piclist){
							for(var i=0;i<feed.refer.piclist.length;i++){
								var w;
								var h;
								if(feed.refer.piclist[i].thumb_w && feed.refer.piclist[i].thumb_h){
									h = "100px";
									w = Math.round(feed.refer.piclist[i].thumb_w*100/feed.refer.piclist[i].thumb_h)+"px";
								}else{
									w = "auto";
									h = "100px";
								}
								template.find(".feed-refer .feed-piclist ul").append("<li><a href=\""+feed.refer.piclist[i].url+"\"><img class='magnify' src='"+feed.refer.piclist[i].url_thumb+"' title='"+feed.refer.piclist[i].pic_name+"' style='width:"+w+";height:"+h+";'/></a></li>");
							}
							template.find(".feed-refer .feed-piclist ul a").css("cursor","url(styles/big.cur),auto");
							template.find(".feed-refer .feed-piclist ul a").fancyzoom();
						}else{
							template.find(".feed-refer .feed-piclist").remove();
						}
						template.find(".feed-refer .att_publish_time").attr("href","u.php?feed="+feed.refer.id);
						template.find(".feed-refer .att_publish_time").html(feed.refer.publish_time);
						
					}else{
						template.find(".feed-refer").empty();
						template.find(".feed-refer").html("<p style='text-align:center'>该条分享不存在或已被删除</p>");
					}
				}else{//原创资源
					template.find(".feed-refer").remove();
					
					if(feed.reslist){
						for(var i=0;i<feed.reslist.length;i++){
							var li = $("<li></li>");
							var img = $("<img/>");
							var a = $("<a></a>");
							
							if(feed.reslist[i].lx=='torrent'){
								img.attr("src",data.weburl+"/resource/res_torrent_20.png");
							}else if(feed.reslist[i].lx=='ed2k'){
								img.attr("src",data.weburl+"/resource/res_ed2k_20.png");
							}else if(feed.reslist[i].lx=='magnet'){
								img.attr("src",data.weburl+"/resource/res_magnet_20.png");
							}else if(feed.reslist[i].lx=='thunder'){
								img.attr("src",data.weburl+"/resource/res_thunder_20.png");
							}
							a.attr("href","download.php?id="+feed.reslist[i].id+"&feedid="+feed.id);
							a.attr("target","_blank");
							a.text(feed.reslist[i].name);
							
							li.append(img);
							li.append(a);
							if(feed.reslist[i].size){
								li.append("<font color=gray>&nbsp;("+feed.reslist[i].size+"&nbsp;"+feed.reslist[i].download+"次下载)</font>");
							}else{
								li.append("<font color=gray>&nbsp;("+feed.reslist[i].download+"次下载)</font>");
							}
							
							template.find(".feed-reslist-cur").append(li);
						}
					}
					if(feed.piclist){
						for(var i=0;i<feed.piclist.length;i++){
							var w;
							var h;
							if(feed.piclist[i].thumb_w && feed.piclist[i].thumb_h){
								h = "100px";
								w = Math.round(feed.piclist[i].thumb_w*100/feed.piclist[i].thumb_h)+"px";
							}else{
								w = "auto";
								h = "100px";
							}
							template.find(".feed-piclist-cur ul").append("<li><a href=\""+feed.piclist[i].url+"\"><img class='magnify' src='"+feed.piclist[i].url_thumb+"' title='"+feed.piclist[i].pic_name+"' style='width:"+w+";height:"+h+";'/></a></li>");
						}
					}
					if(feed.tags){
						for(var i=0;i<feed.tags.length;i++){
							template.find(".tags-cur").append("<a href='"+data.weburl+"/search.php?lx=all&sj=all&skey="+feed.tags[i]+"'>"+feed.tags[i]+"</a>");
						}
					}else{
						template.find(".tags-cur").remove();
					}
				}
			}else{
				template.html("<p style='text-align:center;background-color:#f7f7f7;padding:20px 10px;'>该条分享不存在或已被删除</p>");
			}
			template.css("display","block");
			if($("#feeds #feeds-tip").css("display")=="block"){
				$("#feeds #feeds-tip").css("display","none");
			}
		});
	}
}
*/
/*
function pagenav(url,page,pagesize,total){
	page = parseInt(page);
	pagesize = parseInt(pagesize);
	total = parseInt(total);
	var totalpage = Math.ceil(total/pagesize);
	if(totalpage>=2){
		var html = "";
		if(totalpage<=5){
			for(var k=1;k<=totalpage;k++){
				if(k==page){
					html+="<a href='#u' class='p_s' onclick='feeds(\""+url+"&page="+k+"\",\"\");return false;'>"+k+"</a>";
				}else{
					html+="<a href='#u' onclick='feeds(\""+url+"&page="+k+"\",\"\");return false;'>"+k+"</a>";
				}
			}
		}else{
			if(page-1>2){
				html+="<a href='#u' onclick='feeds(\""+url+"&page="+1+"\",\"\");return false;'>1</a><span>...</span>";
			}
			for(var k=page-2;k<=page;k++){
				if(k<1)continue;
				if(k==page){
					html+="<a hre='#u' class='p_s' onclickf='feeds(\""+url+"&page="+k+"\",\"\");return false;'>"+k+"</a>";
				}else{
					html+="<a href='#u' onclick='feeds(\""+url+"&page="+k+"\",\"\");return false;'>"+k+"</a>";
				}
			}
			for(var k=page+1;k<=totalpage;k++){
				if(k-page<=2){
					html+="<a href='#u' onclick='feeds(\""+url+"&page="+k+"\",\"\");return false;'>"+k+"</a>";
				}
			}
			if(totalpage-page>2){
				html+="<span>...</span><a href='#u' onclick='feeds(\""+url+"&page="+totalpage+"\",\"\");return false;'>"+totalpage+"</a>";
			}
		}
		if(page>1){
			var prepage = page-1;
			html="<a href='#u' onclick='feeds(\""+url+"&page="+prepage+"\",\"\");return false;'>上一页</a>"+html;
		}
		if(page<totalpage){
			var nextpage = page+1;
			html=html+"<a href='#u' onclick='feeds(\""+url+"&page="+nextpage+"\",\"\");return false;'>下一页</a>";
		}
		return html;
	}	
}
*/